{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\Anaconda3_5_0_0\\lib\\site-packages\\h5py\\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.python.framework import ops\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "ops.reset_default_graph()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sess = tf.Session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5.1 3.5 1.4 0.2]\n"
     ]
    }
   ],
   "source": [
    "risi = datasets.load_iris()\n",
    "print(risi.data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "target = np.array([[1,0] if x == 0 else [0,1] for x in risi.target])\n",
    "# input_x = np.array([[x[2],x[3]] for x in risi.data])\n",
    "input_x = risi.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[133 132  65  95 137  15  59  22  35  82 125 148  84  96  33  27 122  94\n",
      "  78 140  41  11  19  97  86 135 139  12  73  36  89 131  37  74  92 102\n",
      "  30 146 134  50   5  80  72  69  26 120 106 147  70  16 111  45 141  60\n",
      "  91 101  93   0 143  66  39 138 107 110  13  40 142  31 108  85  47  17\n",
      "  23 119   8  54  88 115 127 126  57  18   1 103  52  10 145  43  71  67\n",
      " 136  64 144 118  24  34  32  98   4  81 104  14  58  25  63 117  99 112\n",
      " 113 114  38  75  20  29   2 130  55   6  49  21]\n",
      "[128 129   3   7   9 149  28  42  44  46  48  51  53  56  61  62  68  76\n",
      "  77  79  83  87  90 100 105 109 116 121 123 124]\n"
     ]
    }
   ],
   "source": [
    "train_data_index = np.random.choice(len(input_x),round(len(input_x) *0.8),replace=False )\n",
    "print(train_data_index)\n",
    "train_data = input_x[train_data_index]\n",
    "train_target = target[train_data_index]\n",
    "\n",
    "test_index = np.array(list(set(range(len(input_x))) - set(train_data_index)))\n",
    "print(test_index)\n",
    "test_data = input_x[test_index]\n",
    "test_target = target[test_index]\n",
    "\n",
    "\n",
    "# print(train_data)\n",
    "# print(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "? tf.nn.softmax_cross_entropy_with_logits_v2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#创建模型\n",
    "SIZE_INPUT = 4\n",
    "SIZE_HIDE = 4\n",
    "SIZE_OUTPUT = 2\n",
    "\n",
    "SIZE_BATCH = 20\n",
    "\n",
    "input_x = tf.placeholder(shape=[None,SIZE_INPUT],dtype=tf.float32,name=\"x\")\n",
    "input_y = tf.placeholder(shape=[None,SIZE_OUTPUT],dtype=tf.float32,name=\"y\")\n",
    "\n",
    "\n",
    "\n",
    "# out_1 = tf.layers.dense(input_x,units=10,activation=tf.nn.relu,kernel_initializer=tf.random_normal_initializer,\n",
    "#                         bias_initializer=tf.random_normal_initializer,name=\"layer1\")\n",
    "w1 = tf.Variable(tf.random_normal(shape=[SIZE_INPUT,SIZE_HIDE]),name=\"w1\")\n",
    "b1 = tf.Variable(tf.random_normal(shape=[SIZE_HIDE]),name=\"bias1\")\n",
    "out_1 =tf.nn.relu(tf.matmul(input_x,w1) + b1,name=\"out1\")\n",
    "\n",
    "# out_2 = tf.layers.dense(out_1,units=SIZE_OUTPUT,activation=tf.nn.softmax,kernel_initializer=tf.random_normal_initializer,\n",
    "#                         bias_initializer=tf.random_normal_initializer,name=\"layer2\")\n",
    "\n",
    "w2 = tf.Variable(tf.random_normal(shape=[SIZE_HIDE,SIZE_OUTPUT]),name=\"w2\")\n",
    "b2 = tf.Variable(tf.random_normal(shape=[SIZE_OUTPUT]),name=\"bias2\")\n",
    "out_2 =tf.nn.softmax( tf.add (tf.matmul(out_1,w2) , b2,name=\"out2\"))\n",
    "\n",
    "# cross_entropy = tf.reduce_mean(out_2 * tf.log(input_y))\n",
    "# loss\n",
    "loss =tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=input_y,logits=out_2,name=\"loss\"))\n",
    "opt = tf.train.GradientDescentOptimizer(0.001)\n",
    "train_step = opt.minimize(loss,name=\"train\")\n",
    "\n",
    "correct_prediction = tf.equal(tf.argmax(out_2,axis=1),tf.argmax(input_y,axis=1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc =  0.73333335  loss =  0.580105  cp =  [ True  True False False False  True False False False False False  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  0.73333335  loss =  0.580056  cp =  [ True  True False False False  True False False False False False  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  0.73333335  loss =  0.5800288  cp =  [ True  True False False False  True False False False False False  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  0.73333335  loss =  0.5800016  cp =  [ True  True False False False  True False False False False False  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  0.73333335  loss =  0.57997996  cp =  [ True  True False False False  True False False False False False  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  0.73333335  loss =  0.5794745  cp =  [ True  True False False False  True False False False False False  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  1.0  loss =  0.3545381  cp =  [ True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  1.0  loss =  0.32056195  cp =  [ True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  1.0  loss =  0.3172285  cp =  [ True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "acc =  1.0  loss =  0.31598404  cp =  [ True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n"
     ]
    }
   ],
   "source": [
    "for i in range(10000):\n",
    "    random_index = np.random.choice(len(train_data),SIZE_BATCH)\n",
    "    x = train_data[random_index]\n",
    "    y = train_target[random_index]\n",
    "    \n",
    "    sess.run(train_step,feed_dict={input_x:x,input_y:y})\n",
    "    \n",
    "    if(i+1) % 1000 == 0:\n",
    "        x = test_data\n",
    "        y = test_target\n",
    "#         l = sess.run(loss,feed_dict={input_x:x,input_y:y})\n",
    "        l = sess.run(loss,feed_dict={input_x:x,input_y:y})\n",
    "        cp = sess.run(correct_prediction,feed_dict={input_x:x,input_y:y})\n",
    "        acc = sess.run(accuracy,feed_dict={input_x:x,input_y:y})\n",
    "        print(\"acc = \",acc,\" loss = \",l,\" cp = \",cp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.summary.writer.writer.FileWriter at 0x19c73476940>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.summary.FileWriter(\"./tmp/logs\",sess.graph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.65764913e-06 9.99996305e-01]\n",
      " [1.04170605e-04 9.99895811e-01]\n",
      " [9.87877488e-01 1.21224988e-02]\n",
      " [9.92365420e-01 7.63459830e-03]\n",
      " [9.94477332e-01 5.52270329e-03]\n",
      " [7.68198734e-05 9.99923229e-01]\n",
      " [9.95737791e-01 4.26223828e-03]\n",
      " [9.76533473e-01 2.34665815e-02]\n",
      " [9.85164881e-01 1.48351686e-02]\n",
      " [9.86172855e-01 1.38271106e-02]\n",
      " [9.93704140e-01 6.29589008e-03]\n",
      " [3.22726043e-03 9.96772707e-01]\n",
      " [1.11271278e-03 9.98887241e-01]\n",
      " [1.59794290e-03 9.98402059e-01]\n",
      " [2.53568031e-03 9.97464299e-01]\n",
      " [5.44770621e-03 9.94552314e-01]\n",
      " [1.37811716e-04 9.99862194e-01]\n",
      " [1.08600024e-03 9.98914003e-01]\n",
      " [2.42355731e-04 9.99757588e-01]\n",
      " [4.28838059e-02 9.57116246e-01]\n",
      " [9.70311739e-05 9.99902964e-01]\n",
      " [7.25870254e-04 9.99274194e-01]\n",
      " [2.12489231e-03 9.97875094e-01]\n",
      " [7.80649373e-07 9.99999166e-01]\n",
      " [1.56294902e-06 9.99998450e-01]\n",
      " [2.47648813e-06 9.99997497e-01]\n",
      " [4.54073925e-05 9.99954581e-01]\n",
      " [1.80074094e-05 9.99981999e-01]\n",
      " [5.63030189e-05 9.99943733e-01]\n",
      " [1.59301162e-05 9.99984026e-01]]\n",
      "[1 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]\n",
      "[1 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]\n",
      "[ True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True  True  True  True  True  True  True\n",
      "  True  True  True  True  True  True]\n",
      "[[0 1]\n",
      " [0 1]\n",
      " [1 0]\n",
      " [1 0]\n",
      " [1 0]\n",
      " [0 1]\n",
      " [1 0]\n",
      " [1 0]\n",
      " [1 0]\n",
      " [1 0]\n",
      " [1 0]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]\n",
      " [0 1]]\n"
     ]
    }
   ],
   "source": [
    "x = test_data\n",
    "y = test_target\n",
    "out = sess.run(out_2,feed_dict={input_x:x,input_y:y})\n",
    "print(out)\n",
    "print(sess.run(tf.argmax(out,axis=1)))\n",
    "print(np.argmax(y,axis=1))\n",
    "# print(sess.run(tf.argmax(input_y,axis=1)),feed_dict={input_y:y})\n",
    "print(sess.run(correct_prediction,feed_dict={input_x:x,input_y:y}))\n",
    "print(test_target)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
